<?php

/**
 * SocialEnginePro
 *
 * @category   Application_Extensions
 * @package    money
 * @author     Azim
 */

/**
 * @category   Application_Extensions
 * @package    money
 */
class Engine_Payment_Gateway_LiqPay extends Engine_Payment_Gateway {

    // Support

    protected $_supportedCurrencies = array(
        // 'ARS', // Not supported by PayPal
        //  'AUD',
        //  'BRL',
        //  'CAD',
        //   'CHF',
        //'CZK', // Supported by PayPal, but not by 2Checkout
        //  'DKK',
        'EUR',
        // 'GBP',
        //'HUF', // Supported by PayPal, but not by 2Checkout
        // 'HKD',
        //'MYR', // Supported by PayPal, but not by 2Checkout
        //'ILS', // Supported by PayPal, but not by 2Checkout
        // 'INR', // Not supported by PayPal
        // 'JPY',
        // 'MXN',
        // 'NOK',
        // 'NZD',
        //'PHP', // Supported by PayPal, but not by 2Checkout
        //'PLN', // Supported by PayPal, but not by 2Checkout
        // 'SEK',
        //'SGD', // Supported by PayPal, but not by 2Checkout
        //'TWD', // Supported by PayPal, but not by 2Checkout
        //'THB', // Supported by PayPal, but not by 2Checkout
        'USD',
        // 'ZAR', // Not supported by PayPal
        'RUB', // Not supported by PayPal, 2Checkout
        'UAH' // Not supported by PayPal, 2Checkout
    );
    protected $_supportedLanguages = array(
        'ru', 'en', 'uk',
            // Full
            //'es_AR', 'en_AU', 'de_AT', 'en_BE', 'fr_BE', 'nl_BE', 'pt_BR', 'en_CA',
            //'fr_CA', 'zh_CN', 'zh_HK', 'fr_FR', 'de_DE', 'it_IT', 'ja_JP', 'es_MX',
            //'nl_NL', 'pl_PL', 'en_SG', 'es_SP', 'fr_CH', 'de_CH', 'en_CH', 'en_GB',
            //'en_US',
            // Not supported
            //'de_BE', 'zh_SG', 'gsw_CH', 'it_CH', 
    );
    protected $_supportedRegions = array(
        'AF', 'AX', 'AL', 'DZ', 'AS', 'AD', 'AO', 'AI', 'AQ', 'AG', 'AR', 'AM',
        'AW', 'AU', 'AT', 'AZ', 'BS', 'BH', 'BD', 'BB', 'BY', 'BE', 'BZ', 'BJ',
        'BM', 'BT', 'BO', 'BA', 'BW', 'BV', 'BR', 'IO', 'BN', 'BG', 'BF', 'BI',
        'KH', 'CM', 'CA', 'CV', 'KY', 'CF', 'TD', 'CL', 'CN', 'CX', 'CC', 'CO',
        'KM', 'CG', 'CD', 'CK', 'CR', 'CI', 'HR', 'CU', 'CY', 'CZ', 'DK', 'DJ',
        'DM', 'DO', 'EC', 'EG', 'SV', 'GQ', 'ER', 'EE', 'ET', 'FK', 'FO', 'FJ',
        'FI', 'FR', 'GF', 'PF', 'TF', 'GA', 'GM', 'GE', 'DE', 'GH', 'GI', 'GR',
        'GL', 'GD', 'GP', 'GU', 'GT', 'GG', 'GN', 'GW', 'GY', 'HT', 'HM', 'VA',
        'HN', 'HK', 'HU', 'IS', 'IN', 'ID', 'IR', 'IQ', 'IE', 'IM', 'IL', 'IT',
        'JM', 'JP', 'JE', 'JO', 'KZ', 'KE', 'KI', 'KP', 'KR', 'KW', 'KG', 'LA',
        'LV', 'LB', 'LS', 'LR', 'LY', 'LI', 'LT', 'LU', 'MO', 'MK', 'MG', 'MW',
        'MY', 'MV', 'ML', 'MT', 'MH', 'MQ', 'MR', 'MU', 'YT', 'MX', 'FM', 'MD',
        'MC', 'MN', 'MS', 'MA', 'MZ', 'MM', 'NA', 'NR', 'NP', 'NL', 'AN', 'NC',
        'NZ', 'NI', 'NE', 'NG', 'NU', 'NF', 'MP', 'NO', 'OM', 'PK', 'PW', 'PS',
        'PA', 'PG', 'PY', 'PE', 'PH', 'PN', 'PL', 'PT', 'PR', 'QA', 'RE', 'RO',
        'RU', 'RW', 'SH', 'KN', 'LC', 'PM', 'VC', 'WS', 'SM', 'ST', 'SA', 'SN',
        'CS', 'SC', 'SL', 'SG', 'SK', 'SI', 'SB', 'SO', 'ZA', 'GS', 'ES', 'LK',
        'SD', 'SR', 'SJ', 'SZ', 'SE', 'CH', 'SY', 'TW', 'TJ', 'TZ', 'TH', 'TL',
        'TG', 'TK', 'TO', 'TT', 'TN', 'TR', 'TM', 'TC', 'TV', 'UG', 'UA', 'AE',
        'GB', 'US', 'UM', 'UY', 'UZ', 'VU', 'VE', 'VN', 'VG', 'VI', 'WF', 'EH',
        'YE', 'ZM',
    );
    protected $_supportedBillingCycles = array(
        /* 'Day', */ 'Week', /* 'SemiMonth', */ 'Month', 'Year',
    );
    // Translation

    protected $_transactionMap = array(
        Engine_Payment_Transaction::REGION => 'LOCALECODE',
        Engine_Payment_Transaction::RETURN_URL => 'RETURNURL',
        Engine_Payment_Transaction::CANCEL_URL => 'CANCELURL',
        // Deprecated?
        Engine_Payment_Transaction::IPN_URL => 'NOTIFYURL',
        Engine_Payment_Transaction::VENDOR_ORDER_ID => 'INVNUM',
        Engine_Payment_Transaction::CURRENCY => 'CURRENCYCODE',
        Engine_Payment_Transaction::REGION => 'LOCALECODE',
    );

    // General

    /**
     * Constructor
     *
     * @param array $options
     */
    public function __construct(array $options = null) {
        parent::__construct($options);

        if (null === $this->getGatewayMethod()) {
            $this->setGatewayMethod('POST');
        }
    }

    /**
     * Get the service API
     *
     * @return Engine_Service_PayPal
     */
    public function getService() {
        if (null === $this->_service) {
            $this->_service = new Engine_Service_LiqPay(array_merge(
                                    $this->getConfig(), array(
                                'testMode' => $this->getTestMode()
                                    )
                    ));
        }

        return $this->_service;
    }

    public function getGatewayUrl() {
        // Manual
        if (null !== $this->_gatewayUrl) {
            return $this->_gatewayUrl;
        }

        // Auto
        if ($this->getTestMode()) {
            return 'https://www.liqpay.com/?do=clickNbuy';
        } else {
            return 'https://www.liqpay.com/?do=clickNbuy';
        }
    }

    // IPN

    public function processIpn(Engine_Payment_Ipn $ipn) {
        // Validate ----------------------------------------------------------------
        // Get raw data
        $rawData = $ipn->getRawData();

        // Log raw data
        //if( 'development' === APPLICATION_ENV ) {
        $this->_log(print_r($rawData, true), Zend_Log::DEBUG);
        //}
        // Check a couple things in advance
        if (!empty($rawData['test_ipn']) && !$this->getTestMode()) {
            throw new Engine_Payment_Gateway_Exception('Test IPN sent in non-test mode');
        }

        // @todo check the email address of the account?
        // Build url and post data
        $parsedUrl = parse_url($this->getGatewayUrl());
        $postString = http_build_query(array_merge(array(
                    'cmd' => '_notify-validate',
                        ), $rawData));

        if (empty($parsedUrl['host'])) {
            $this->_throw(sprintf('Invalid host in gateway url: %s', $this->getGatewayUrl()));
            return false;
        }
        if (empty($parsedUrl['path'])) {
            $parsedUrl['path'] = '/';
        }

        // Open socket
        $fp = fsockopen($parsedUrl['host'], 80, $errNum, $errStr, 30);
        if (!$fp) {
            $this->_throw(sprintf('Unable to open socket: [%d] %s', $errNum, $errStr));
        }
        stream_set_blocking($fp, true);

        fputs($fp, "POST {$parsedUrl['path']} HTTP/1.1\r\n");
        fputs($fp, "Host: {$parsedUrl['host']}\r\n");
        fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
        fputs($fp, "Content-length: " . strlen($postString) . "\r\n");
        fputs($fp, "Connection: close\r\n\r\n");
        fputs($fp, $postString . "\r\n\r\n");

        $response = '';
        while (!feof($fp)) {
            $response .= fgets($fp, 1024);
        }

        fclose($fp);

        if (!stripos($response, 'VERIFIED')) {
            $this->_throw(sprintf('IPN Validation Failed: %s %s', $parsedUrl['host'], $parsedUrl['path']));
            return false;
        }

        // Success!
        $this->_log('IPN Validation Succeeded');



        // Process -----------------------------------------------------------------
        $rawData = $ipn->getRawData();

        $data = $rawData;

        return $data;
    }

    // Transaction

    public function processTransaction(Engine_Payment_Transaction $transaction) {
        $data = array();
        $rawData = $transaction->getRawData();

        // Driver-specific params
        if (isset($rawData['driverSpecificParams'])) {
            if (isset($rawData['driverSpecificParams'][$this->getDriver()])) {
                $data = array_merge($data, $rawData['driverSpecificParams'][$this->getDriver()]);
            }
            unset($rawData['driverSpecificParams']);
        }

        // Add default region?
        if (empty($rawData['region']) && ($region = $this->getRegion())) {
            $rawData['region'] = $region;
        }

        // Add default currency
        if (empty($rawData['currency']) && ($currency = $this->getCurrency())) {

            $rawData['currency'] = $currency;
        }



        // Process abtract translation map
        $tmp = array();
        $data = array_merge($data, $this->_translateTransactionData($rawData, $tmp));
        $rawData = $tmp;
        
        
        $sigXml = array();
        
        $sigXml['signature'] = $this->getService()->getSignature($data);
        $sigXml['operation_xml'] = $this->getService()->getOperationXml($data);
        

        return $sigXml;
    }

    // Admin

    public function test() {
        //@todo Accounty info
        //  $this->getService()->searchButtons(time());

        return true;
    }

}
